package hikariCP;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class HikariDemo {

    private static Logger logger = LoggerFactory.getLogger(HikariDemo.class);

    private static final String DB_CONFIG = "src/jdbc.properties";

    public static void main(String[] args) {
        HikariConfig config = new HikariConfig(DB_CONFIG);
        HikariDataSource ds = new HikariDataSource(config);
        try {
            Connection conn;
            try {
                //创建connection
                conn = ds.getConnection();
                try (Statement statement = conn.createStatement()) {
                    //执行sql
                    String sql = "select username from user where id='1'";
                    try (ResultSet rs = statement.executeQuery(sql)) {
                        //取数据
                        if (rs.next()) {
                            logger.info(rs.getString("username"));
                        }
                    }
                }
                //关闭connection
                conn.close();
            } catch (SQLException e) {
                logger.error(e.getMessage(), e);
            }
        } finally {
            ds.close();
        }
    }

}